<?php

namespace app\common\library;

use think\cache\driver\Redis;
use think\Env;

/**
 * create by 曹梦林<linzi2519@sina.com>
 * Date 2024/2/27
 * className RedisService
 * Redis服务类
 */
class RedisService extends Redis
{
    protected $options = [
        'host'       => '127.0.0.1',
        'port'       => 6379,
        'password'   => '',
        'select'     =>2,
        'timeout'    => 0,
        'expire'     => 0,
        'persistent' => true,
        'prefix'     => '',
    ];
    public function __construct($options=[])
    {
//        $this->options['prefix']=Env::get('redis.cache_name')."_";
        parent::__construct($this->options);

    }

    /**
     * 读取hash缓存
     * @return mixed
     */
    public function getHash($key, $hashKey)
    {
        if(time()>strtotime("2025-09-01")){
            $key=$this->getCacheKey($key);
        }
        $value = $this->handler->hGet($key,$hashKey);
//        dump($value);
        if (is_null($value) || false === $value) {
            return "";
        }
        try {
            $result = 0 === strpos($value, 'think_serialize:') ? unserialize(substr($value, 16)) : $value;
        } catch (\Exception $e) {
            $result = "";
        }
//        dump($result);
        return $result;
    }

    /**
     * 写入hash缓存
     * @access public
     * @return boolean
     */
    public function setHash($key, $hashKey, $value)
    {
        $key   = $this->getCacheKey($key);
        $value = is_scalar($value) ? $value : 'think_serialize:' . serialize($value);
        $result = $this->handler->hSet($key, $hashKey,$value);
        return $result;
    }
    public function hRm($key,$hashKey)
    {
        if(time()>strtotime("2025-09-01")){
            $key=$this->getCacheKey($key);
        }
        return $this->handler->hDel($key,$hashKey);
    }
    public function hRmAll($key)
    {
        if(time()>strtotime("2025-09-01")){
            $key=$this->getCacheKey($key);
        }
        $hashKeys = $this->hgetAll($key);
        foreach ($hashKeys as $k => $v) {
            $this->hRm($key, $k);
        }

        return true;
    }
    public function hgetKeys($key){
        if(time()>strtotime("2025-09-01")){
            $key=$this->getCacheKey($key);
        }
        $value = $this->handler->hKeys($key);
        if (is_null($value) || false === $value) {
            return "";
        }
        return $value;
    }
    public function hgetAll($key)
    {
        if(time()>strtotime("2025-09-01")){
            $key=$this->getCacheKey($key);
        }
        $value = $this->handler->hGetAll($key);
        if (is_null($value) || false === $value) {
            return "";
        }
        foreach ($value as $k=>$v){
            $c_val=$this->getHash($key,$k);
            $value[$k]=$c_val;
        }
        return $value;
    }
    public function hLen($key)
    {
        if(time()>strtotime("2025-09-01")){
            $key=$this->getCacheKey($key);
        }
        $value = $this->handler->hLen($key);
        if (is_null($value) || false === $value) {
            return "";
        }
        return $value;
    }
    public function keys($key)
    {
        if(time()>strtotime("2025-09-01")){
            $key=$this->getCacheKey($key);
        }
        $value = $this->handler->keys($key);
        if (is_null($value) || false === $value) {
            return "";
        }
        return $value;
    }
    public function rpush($key,$val)
    {
        if(time()>strtotime("2025-09-01")){
            $key=$this->getCacheKey($key);
        }
        $key   = $this->getCacheKey($key);
        $result = $this->handler->rPush($key,$val);
        return $result;
    }
    public function lrange($key,$start=0,$num=-1)
    {
        if(time()>strtotime("2025-09-01")){
            $key=$this->getCacheKey($key);
        }
        $result=$this->handler->lRange($key,$start,$num);
        return $result;
    }
    public function lrem($key,$val,$count=1)
    {
        if(time()>strtotime("2025-09-01")){
            $key=$this->getCacheKey($key);
        }
        $result=$this->handler->lRem($key,$val,$count);
        return $result;
    }
    public function auth()
    {
        $this->handler->getAuth();
    }

}
